﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using CUWo.Common.Entities;
using Microsoft.Phone.Info;

/**
 * 创建时间：2014/11/3 14:23:51
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace CUWo.Common
{
    public sealed class DeviceInfoHelper
    {
        /// <summary>
        /// 获取设备的IMEI
        /// <para>如果取不到时就用Title+Version+PublisherId组成IMEI号，T:titleV:versionP:publisherId</para>
        /// </summary>
        public static string GetIMEI
        {
            get
            {                
                object value = null;
                //需要ID_CAP_IDENTITY_DEVICE权限
                if (!DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out value))
                {
                    var item = AppHelper.GetWMAppManifestInfo;
                    return string.Concat("T:", item.ApplicationTitle, "V:", item.ApplicationVersion, "P:", item.ApplicationPublisherId);
                }
                string imei = string.Empty;
                byte[] buffer = value as byte[];
                if (buffer != null && buffer.Length > 0)
                {
                    string tmp = string.Empty;
                    foreach (byte num in buffer)
                    {
                        tmp = num.ToString();
                        if (1 == tmp.Length)
                        {
                            tmp = "00" + tmp;
                        }
                        else if (2 == tmp.Length)
                        {
                            tmp = "0" + tmp;
                        }
                        imei = imei + tmp;
                    }
                }
                return imei;
            }
        }

        public static string GetDeviceManufacturer
        {
            get
            {
                return DeviceStatus.DeviceManufacturer;
            }
        }
        public static string GetDeviceName
        {
            get
            {
                return DeviceStatus.DeviceName;
            }
        }

        public static double GetDeviceScreenWidth
        {
            get
            {
                return Application.Current.Host.Content.ActualWidth;
            }
        }

        public static double GetDeviceScreenHeight
        {
            get
            {
                return Application.Current.Host.Content.ActualHeight;
            }
        }

        public static string GetDeviceFirmwareVersion
        {
            get
            {
                return DeviceStatus.DeviceFirmwareVersion;
            }
        }
        public static string GetDeviceHardwareVersion
        {
            get
            {
                return DeviceStatus.DeviceHardwareVersion;
            }
        }
        public static long GetDeviceTotalMemory
        {
            get
            {
                return DeviceStatus.DeviceTotalMemory;
            }
        }

        public static long GetApplicationCurrentMemoryUsage
        {
            get { return DeviceStatus.ApplicationCurrentMemoryUsage; }
        }

        public static string GetCurrentCulture
        {
            get
            {
                return CultureInfo.CurrentCulture.Name;
            }
        }

        public static string GetDeviceOSPlatform
        {
            get { return Environment.OSVersion.Platform.ToString(); }
        }

        public static string GetDeviceOS
        {
            get { return Environment.OSVersion.ToString(); }
        }

        public static string GetDeviceOSVersion
        {
            get
            {
                return Environment.OSVersion.Version.ToString();
            }
        }

        public static string ConvertMB(long value)
        {
            return Math.Round(value / (1024.0 * 1024.0), 2) + "M";
        }
    }
}
